package com.sunny.ynhz.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.sunny.ynhz.R;
import com.sunny.ynhz.ui.SelectFileActivity;
import com.sunny.ynhz.utils.OpenFileUtils;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by luao on 2017/8/18.
 */
public class FileTypeMoreStyleAdapter extends RecyclerView.Adapter<FileTypeMoreStyleAdapter.ViewHolder> {

    private ArrayList<String> data = null;
    private Context context;


    public FileTypeMoreStyleAdapter(Context context, ArrayList<String> data) {
        this.data = data;
        this.context = context;

    }

    //创建新View，被LayoutManager所调用
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item_pro_file, viewGroup, false);
        ViewHolder vh = new ViewHolder(view);
        return vh;
    }

    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(ViewHolder viewHolder, final int position) {
        //取data的时候防止数组越界
        if (position == data.size()) {
            viewHolder.del_image.setVisibility(View.GONE);
            viewHolder.tv_file_name.setText("");
            Glide.with(context)
                    .load(R.mipmap.icon_add_image)
                    .centerCrop()
                    .into(viewHolder.image);
            viewHolder.content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Activity context = (Activity) FileTypeMoreStyleAdapter.this.context;
                     Intent intent=new Intent(context, SelectFileActivity.class);
                    intent.putStringArrayListExtra("data",data);
                    context.startActivityForResult(intent,205);
                }
            });
        } else {
            final String string = data.get(position);
           if(string.contains(".doc") || string.contains(".docx")){
                viewHolder.image.setImageResource(R.mipmap.icon_word);
            }else if(string.contains(".xls") || string.contains(".xlsx")){
                viewHolder.image.setImageResource(R.mipmap.icon_excel);
            }else if(string.contains(".pdf")){
                viewHolder.image.setImageResource(R.mipmap.icon_pdf);
            }
            File file=new File(string);
            viewHolder.tv_file_name.setText(file.getName().substring(0,file.getName().indexOf(".")));
            viewHolder.del_image.setVisibility(View.VISIBLE);
            viewHolder.content.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(string.contains(".doc")){
                        OpenFileUtils.startOpenWordFile(string,context);
                    }
                    else if(string.contains(".pdf")){
                        OpenFileUtils.startOpenpdfFile(string,context);
                    }
                    else if(string.contains("xls")){
                        OpenFileUtils.startOpenExcelFile(string,context);

                    }
                }
            });
            viewHolder.del_image.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    data.remove(position);
                    notifyDataSetChanged();
                }
            });

        }
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return data.size() + 1;
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView image;
        public ImageView del_image;
        public RelativeLayout content;
        TextView tv_file_name;

        public ViewHolder(View view) {
            super(view);
            image =  view.findViewById(R.id.image);
            del_image = view.findViewById(R.id.del_image);
            content =view.findViewById(R.id.content);
            tv_file_name=view.findViewById(R.id.tv_file_name);
        }
    }


}